using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ExpertSMS.Models;

namespace ExpertSMS.Controllers
{   
    public class BillOfLadingProductsController : Controller
    {
        private ExpertSMSContext context = new ExpertSMSContext();

        public ActionResult _BillOfLadingProductDetails(int billOfLadingID)
        {
            ViewBag.PossibleProducts = context.Products;
            return PartialView(billOfLadingID);
        }

        
        [HttpPost]
        public ActionResult _BillOfLadingProductDetails(int productID,decimal? cost,decimal? quantity,int billOfLadingID)
        {
            Product product = context.Products.Single(x => x.ProductID == productID);
            ViewBag.PossibleProducts = product;
            Unit unit = product.Units.FirstOrDefault(x=>!x.ParentUnitID.HasValue);
            ViewBag.PossibleUnit = unit;
            
            return PartialView(billOfLadingID);
        }
        [HttpPost]
        public ActionResult _BillOfLadingProductList(int billOfLadingID)
        {
            return PartialView(context.BillOfLadings.Single(x=>x.BillOfLadingID == billOfLadingID).BillOfLadingProducts);
        }
        
        public ViewResult Index()
        {
            return View(context.BillOfLadingProducts.Include(billofladingproduct => billofladingproduct.BillOfLading).Include(billofladingproduct => billofladingproduct.Product).Include(billofladingproduct => billofladingproduct.Unit).ToList());
        }

        //
        // GET: /BillOfLadingProducts/Details/5

        public ViewResult Details(int id)
        {
            BillOfLadingProduct billofladingproduct = context.BillOfLadingProducts.Single(x => x.BillOfLadingProductID == id);
            return View(billofladingproduct);
        }

        //
        // GET: /BillOfLadingProducts/Create

        public ActionResult Create(int billOfLadingID)
        {
            ViewBag.BillOfLadingID = billOfLadingID;
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleUnits = context.Units;
            return View();
        } 

        //
        // POST: /BillOfLadingProducts/Create

        [HttpPost]
        public ActionResult Create(BillOfLadingProduct billofladingproduct)
        {
            if (ModelState.IsValid)
            {
                context.BillOfLadingProducts.Add(billofladingproduct);
                context.SaveChanges();
                context = new ExpertSMSContext();
                return PartialView("_BillOfLadingProductList",context.BillOfLadings.Single(x=>x.BillOfLadingID == billofladingproduct.BillOfLadingID).BillOfLadingProducts);  
            }

            ViewBag.PossibleBillOfLadings = context.BillOfLadings;
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleUnits = context.Units;
            return View(billofladingproduct);
        }
        
        //
        // GET: /BillOfLadingProducts/Edit/5
 
        public ActionResult Edit(int id)
        {
            BillOfLadingProduct billofladingproduct = context.BillOfLadingProducts.Single(x => x.BillOfLadingProductID == id);
            ViewBag.PossibleBillOfLadings = context.BillOfLadings;
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleUnits = context.Units;
            return View(billofladingproduct);
        }

        //
        // POST: /BillOfLadingProducts/Edit/5

        [HttpPost]
        public ActionResult Edit(BillOfLadingProduct billofladingproduct)
        {
            if (ModelState.IsValid)
            {
                context.Entry(billofladingproduct).State = EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PossibleBillOfLadings = context.BillOfLadings;
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleUnits = context.Units;
            return View(billofladingproduct);
        }

        //
        // GET: /BillOfLadingProducts/Delete/5
 
        public ActionResult Delete(int id)
        {
            BillOfLadingProduct billofladingproduct = context.BillOfLadingProducts.Single(x => x.BillOfLadingProductID == id);
            return View(billofladingproduct);
        }

        //
        // POST: /BillOfLadingProducts/Delete/5

        public ActionResult DeleteConfirmed(int id)
        {
            BillOfLadingProduct billofladingproduct = context.BillOfLadingProducts.Single(x => x.BillOfLadingProductID == id);
            context.BillOfLadingProducts.Remove(billofladingproduct);
            context.SaveChanges();
            context = new ExpertSMSContext();
            return PartialView("_BillOfLadingProductList", context.BillOfLadings.Single(x => x.BillOfLadingID == billofladingproduct.BillOfLadingID).BillOfLadingProducts);  
            
        }
    }
}